/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controlador;

import Facade.VoluntarioFacade;
import VO.EstadisticasVO;
import java.util.Calendar;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import vista.estadisticasBean;

/**
 *
 * @author Alan
 */
public class EstadisticasVoluntariosAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String FAILURE = "failure";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        estadisticasBean frm = (estadisticasBean) form;
        EstadisticasVO datosEstad = new EstadisticasVO();
        
        VoluntarioFacade modelo = new VoluntarioFacade();

        //**************************************************
        //VALIDA AÑO
        String aviso = "";
        try{
            int año = Integer.parseInt(frm.getAnio());
            Calendar fecha = Calendar.getInstance();
            int añoActual = fecha.get(Calendar.YEAR);
            
            if(año<1941 || año > añoActual){
                aviso = "<span style='color:red'>El año debe ser entre 1941 y "+añoActual+"</span>";
                request.setAttribute("aviso",aviso);
                return mapping.findForward(FAILURE);
            }
        }catch(Exception e){
            aviso="<span style='color:red'>Debe ingresar un año válido</span>";
            request.setAttribute("aviso",aviso);
            return mapping.findForward(FAILURE);
        }
        //****************************************************

        
        datosEstad.setAnio(frm.getAnio());
        datosEstad.setMes(frm.getMes());

        EstadisticasVO resultadoEstad = new EstadisticasVO();
        resultadoEstad = modelo.cargarEstadisticas(datosEstad);

        if(resultadoEstad != null){
            request.setAttribute("resultadoEstadisticas", resultadoEstad);
            return mapping.findForward(SUCCESS);
        }
        else{
            return mapping.findForward(FAILURE);
        }
    }
}
